window.onload = function () {
    // 获取showshopcar
    let showcarobj = document.getElementsByClassName("showshopcar")[0];
    $.ajax({
        type: "post",
        url: "/js/gwchuixian",
        data: null,
        dataType: "json",
        success: function (data) {
            let s = "";
            for (let i = 0; i < data.length; i++) {
                s += `<div><div class="chahao"><img src="../img/错.png" alt=""><img src="../img/错1.png" onclick="shan(${i})"></div>`
                s += `<div class="tupian"><img src="../img/${data[i].teaImg}" alt=""></div>`
                s += `<div class="mingzi">${data[i].teaName}</div>`
                s += `<div class="jiage" id="jiage${i}">${data[i].teaPrice}</div>`
                s += `<div class="shuliang"><input type="button" value="-" onclick="jian(${i})"><input type="text" value="${data[i].num}" id="zhi${i}"><input type="button" value="+" onclick="jia(${i})"></div>`
                s += `<div class="heji" id="heji${i}">${data[i].teaPrice * data[i].num}</div></div>`
            }
            showcarobj.innerHTML = s;
        }
    })
}

function jian(i) {
    let zhi = document.getElementById("zhi" + i).value;
    document.getElementById("zhi" + i).value = --zhi
    if (zhi < 1) {
        document.getElementById("zhi" + i).value = 1;
    }
    let jiage = document.getElementById("jiage" + i).innerHTML;
    document.getElementById("heji" + i).innerHTML = document.getElementById("zhi" + i).value * jiage
    sum()
}
function jia(i) {
    let zhi = document.getElementById("zhi" + i).value;
    document.getElementById("zhi" + i).value = ++zhi
    let jiage = document.getElementById("jiage" + i).innerHTML;
    document.getElementById("heji" + i).innerHTML = document.getElementById("zhi" + i).value * jiage
    sum()
}
function shan(i) {
    let obj = {
        i: i
    }
    $.ajax({
        type: "post",
        url: "/js/shanchu",
        data: JSON.stringify(obj),
        dataType: "json",
        success: function (data) {
            if (data) {
                location.reload()
            }
        }
    })
}
function sum() {
    let hejilist = document.getElementsByClassName("heji")
    let sum=0;
    for (let i = 0; i < hejilist.length; i++) {
        sum+=Number(hejilist[i].innerText)
    }
    document.getElementById("xiaoji").innerText = sum;
    document.getElementById("zongji").innerText = sum;
}


